import csv
from datetime import datetime

read_file = r"C:\Users\Sinyer\Documents\Chrome History\htu_autobackup_20250228_full.tsv"
write_dir = r"C:\Users\Sinyer\Desktop\Python\history"

h2023 = open(f"{write_dir}\history2023.txt", "w", encoding="utf-8")
h2024 = open(f"{write_dir}\history2024.txt", "w", encoding="utf-8")
h2025 = open(f"{write_dir}\history2025.txt", "w", encoding="utf-8")

file = open(read_file, "r", encoding="utf-8")
lines = csv.reader(file, delimiter='\t')

sort_lines = []
for line in lines:
    name, link, time = line[3], line[0], float(line[1][1:]) / 1000
    sort_lines.append((time, name, link))

file.close()
sorted_lines = sorted(sort_lines, key=lambda x: x[0])
line_count, file_num, output = 0, 1, None
for line in sorted_lines:
    time, name, link = line
    time = datetime.fromtimestamp(time).strftime('%Y-%m-%d %H:%M:%S')
    if time[:4] == "2023": h2023.write(str(" ".join([time[5:], name, link])) + "\n")
    if time[:4] == "2024": h2024.write(str(" ".join([time[5:], name, link])) + "\n")
    if time[:4] == "2025": h2025.write(str(" ".join([time[5:], name, link])) + "\n")

h2023.close()
h2024.close()
h2025.close()
